package com.example.chapter05;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {

    private String mPhone;
    private String mverifyCode;
    private EditText et_enter_password;
    private EditText et_newPassword;
    private EditText et_verifycode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget);
        //从上一个页面获取要修改密码的手机号码
        mPhone = getIntent().getStringExtra("phone");
        findViewById(R.id.btn_verifycode).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        et_newPassword = findViewById(R.id.et_newPassword);
        et_enter_password = findViewById(R.id.et_enter_Password);
        et_verifycode = findViewById(R.id.et_verifycode);

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.btn_verifycode:
                //生成6位随机验证码
                mverifyCode = String.format("%06d", new Random().nextInt(999999));
                //以下弹出提醒对话框，提示用户记住六位数的验证码
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("请记住密码");
                builder.setMessage("手机号" + mPhone + "本次验证码是" + mverifyCode + "请输入验证码");
                builder.setPositiveButton("好的", null);
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            case R.id.btn_confirm:
                // 点击了确定按钮
                String password_first = et_newPassword.getText().toString();
                String password_second = et_enter_password.getText().toString();
                if (password_first.length()<6){
                    Toast.makeText(this, "请输入六位数密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password_first.equals(password_second)){
                    Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mverifyCode.equals(et_verifycode.getText().toString())){
                    Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(this, "修改密码成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("new_password",password_first);
                setResult(Activity.RESULT_OK,intent);
                finish();
                break;


        }

        }
    }
